package com.sharemarking.wa.platform.service.OrderCommonFactory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.Order;
import com.sharemarking.wa.common.entity.OrgMember;
import com.sharemarking.wa.common.eum.OrderOperateTypeEum;
import com.sharemarking.wa.common.eum.OrderStateEum;
import com.sharemarking.wa.common.eum.OrderTypeEum;
import com.sharemarking.wa.platform.dto.orderCommon.OrderInfoDto;
import com.sharemarking.wa.platform.dto.orderCommon.OrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.TaskOrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.WasteListDto;
import com.sharemarking.wa.platform.mapper.*;
import com.sharemarking.wa.platform.service.SecurityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Component
public class OrderManagement implements OrderCommon {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private OrgMemberMapper orgMemberMapper;
    @Autowired
    private DriverVerifyMapper driverVerifyMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderCommonMapper orderCommonMapper;

    @Override
    public List<OrderListDto> getOrderList(Map params) {
        params.put("orgId", orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id", params.get("memberId"))).getOrgId());

        List<OrderListDto> list = orderCommonMapper.getList(params);

        for (OrderListDto o : list) {
            o.setWasteList(orderCommonMapper.getWasteList(o.getId()));
            o.setOrgName(orgMapper.getOrgNameById(o.getSendOrgId()));

            List<OrderOperateTypeEum> operateTypeEums = new ArrayList<>();
            if (o.getState().equals(OrderStateEum.RECEIPT.getKey())) {
                //待经营单位接单 可以取消订单，和接单
                operateTypeEums.add(OrderOperateTypeEum.ACCEPT);
                operateTypeEums.add(OrderOperateTypeEum.CANCEL_ACCEPT);
            } else if (o.getPayer() != null && o.getPayer() == 1 && o.getState().equals(OrderStateEum.UNPAID.getKey())) {
                //支付方是产废单位 状态为待支付 可以取消订单
                operateTypeEums.add(OrderOperateTypeEum.CANCEL_ACCEPT);
            } else if (o.getState().equals(OrderStateEum.UNPAID.getKey()) && (o.getPayer() == null || o.getPayer() == 0)) {
                //待付款 且支付方为null 或者 本身
                operateTypeEums.add(OrderOperateTypeEum.PAY);
                operateTypeEums.add(OrderOperateTypeEum.CANCEL_ACCEPT);
            } else if (o.getState().equals(OrderStateEum.TRANSPORTING.getKey())) {
                //运输中 可以现在完成订单
                operateTypeEums.add(OrderOperateTypeEum.FINISH);
            }
            o.setOperate(operateTypeEums);
        }
        return list;
    }

    @Override
    public OrderInfoDto getOrderInfo(Map params) {
        String code = String.valueOf(params.get("code"));

        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("code", code));
        OrderInfoDto orderInfoDto = new OrderInfoDto();

        BeanUtils.copyProperties(order, orderInfoDto);

        //总单，而且是经营端查看，必然是查询可接订单详情
        if (order.getType().equals(OrderTypeEum.PARENT_TYPE.getKey())) {
            Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id", params.get("memberId"))).getOrgId();
            //获取废品信息
            orderInfoDto.setWasteList(orderCommonMapper.getReceiveOrderWasteList(order.getId(), orgId, null, null,null));

            //获取装货地址
            String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
            orderInfoDto.setLoadingAddress(loadingAddress);

            //设置操作
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            eumList.add(OrderOperateTypeEum.ACCEPT);
            orderInfoDto.setOperate(eumList);

            return orderInfoDto;
        }


        //子订单
        //获取装货地址
        String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
        //获取卸货地址
        String unLoadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 1);

        orderInfoDto.setLoadingAddress(loadingAddress);
        orderInfoDto.setUnLoadingAddress(unLoadingAddress);

        //完成时间 和 到达时间
        orderInfoDto.setArrivalTime(order.getArrivalTime());
        orderInfoDto.setCompleteTime(order.getCompleteTime());

        //获取经营单位名称
        if (!StringUtils.isEmpty(order.getReceiveOrgId())) {
            orderInfoDto.setOrgName(orgMapper.getOrgNameById(order.getReceiveOrgId()));
        }

        //获取经营单位联系人姓名和电话
        orderInfoDto.setCarWeight(order.getCarWeight());
        if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
            Member member = memberMapper.get(order.getReceiveOrgMemberId());
            if (member.getRealName() != null) {
                orderInfoDto.setContact(member.getRealName());
            } else {
                orderInfoDto.setContact(member.getNickName());
            }
            orderInfoDto.setMobile(member.getMobile());
        }

        //获取运输单位名称
        if (!StringUtils.isEmpty(order.getTransportOrgId())) {
            orderInfoDto.setTransportOrgName(orgMapper.getOrgNameById(order.getTransportOrgId()));
        }


        //获取司机名称和电话
        if (!StringUtils.isEmpty(order.getDriverId())) {
            Member member = memberMapper.get(order.getDriverId());
            if (member != null) {
                orderInfoDto.setDriverName(member.getRealName() != null?member.getRealName():member.getNickName());
                orderInfoDto.setDriverMobile(member.getMobile());
            }
            orderInfoDto.setCarWeight(order.getCarWeight());
            orderInfoDto.setCarNumber(driverVerifyMapper.getCarMemberIdByMemberId(order.getDriverId()));
        }

        //获取废品列表
        List<WasteListDto> childOrderWasteListDtos = orderCommonMapper.getChildOrderWasteListByOrderId(order.getId());
        orderInfoDto.setWasteList(childOrderWasteListDtos);

        Member member;
        //获取产废单位信息
        orderInfoDto.setWasteOrgName(orgMapper.getOrgNameById(order.getSendOrgId()));
        //获取产废单位联系人 手机号码
        member = memberMapper.get(order.getSendOrgMemberId());
        if (member.getRealName() != null) {
            orderInfoDto.setWasteContact(member.getRealName());
        } else {
            orderInfoDto.setWasteContact(member.getNickName());
        }
        orderInfoDto.setWasteMobile(member.getMobile());


        List<OrderOperateTypeEum> operateTypeEums = new ArrayList<>();
        if (order.getState().equals(OrderStateEum.RECEIPT.getKey())) {
            //待经营单位接单 可以取消订单，和接单
            operateTypeEums.add(OrderOperateTypeEum.ACCEPT);
            operateTypeEums.add(OrderOperateTypeEum.CANCEL_ACCEPT);
        } else if (order.getPayer() != null && order.getPayer() == 1 && order.getState().equals(OrderStateEum.UNPAID.getKey())) {
            //支付方是产废单位 状态为待支付 可以取消订单
            operateTypeEums.add(OrderOperateTypeEum.CANCEL_ACCEPT);
        } else if (order.getState().equals(OrderStateEum.UNPAID.getKey()) && (order.getPayer() == null || order.getPayer() == 0)) {
            //待付款 且支付方为null 或者 本身
            operateTypeEums.add(OrderOperateTypeEum.PAY);
            operateTypeEums.add(OrderOperateTypeEum.CANCEL_ACCEPT);
        } else if (order.getState().equals(OrderStateEum.TRANSPORTING.getKey())) {
            //运输中 可以现在完成订单
            operateTypeEums.add(OrderOperateTypeEum.FINISH);
        }
        orderInfoDto.setOperate(operateTypeEums);


        return orderInfoDto;
    }

    @Override
    public List<TaskOrderListDto> getTaskOrderList(Map params) {
        //获取可接订单列表
        List<Integer> orderIdList = orderCommonMapper.getManagementTaskList(params);

        List<TaskOrderListDto> orderListDtos = new ArrayList<>();
        for (Integer i : orderIdList) {
            //查询订单信息
            TaskOrderListDto o = orderCommonMapper.getOrderInfoById(i);
            //获取废品可接信息
            o.setWasteList(orderCommonMapper.getReceiveOrderWasteList(i, (Integer) params.get("orgId"), null, null, params.get("searchStr")));
            //设置操作
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            eumList.add(OrderOperateTypeEum.ACCEPT);
            o.setOperate(eumList);
            orderListDtos.add(o);
        }

        return orderListDtos;
    }

    @Override
    public OrderInfoDto getTaskOrderInfo(Map params) {
        String code = (String) params.get("code");

        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("code", code));
        OrderInfoDto orderInfoDto = new OrderInfoDto();

        BeanUtils.copyProperties(order, orderInfoDto);

        //获取废品信息
        orderInfoDto.setWasteList(orderCommonMapper.getReceiveOrderWasteListInfo(order.getId()));

        //获取装货地址
        String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
        orderInfoDto.setLoadingAddress(loadingAddress);

        //设置操作
        List<OrderOperateTypeEum> eumList = new ArrayList<>();
        eumList.add(OrderOperateTypeEum.ACCEPT);
        orderInfoDto.setOperate(eumList);

        return orderInfoDto;
    }
}
